package com.gsit.orc.util;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.net.URL;

import javax.swing.JFrame;

public class MediaPlayerUtil {
	
	 private URL url = null;
	 private MediaPanel mediaPlayer =null;
	 private final static String FILE_NAME= "bailey.mpg";
	 
	 private static JFrame mediaPlayerFrame=null;
	 
	 public MediaPlayerUtil(){
		 url = ResourceLoader.getURL(FILE_NAME);
	 }
	 
	
	 
	 public void createAndRun()throws Exception{
		 if(mediaPlayerFrame == null){
			 mediaPlayerFrame = new JFrame( "OCR Watcher" );
			/*mediaPlayerFrame.addWindowListener(new WindowAdapter() {
				 public void windowClosing(WindowEvent we){
					 System.exit(1);
		          }

			});*/
			 if(mediaPlayer == null){
				 mediaPlayer = new MediaPanel(url,this);
		         mediaPlayerFrame.add( mediaPlayer );
		         reloadFrame();
			 }
		 
		 }
		
		 start();
	 }
	 
	 private void reloadFrame(){
		 if(!mediaPlayerFrame.isActive()){
			 mediaPlayerFrame.setSize( 1000, 700 );
			 mediaPlayerFrame.setAlwaysOnTop(true);
	         mediaPlayerFrame.setVisible( true );
	         mediaPlayerFrame.show();
	         
		 }
		
	 }
	 
	 private void start()throws Exception{
		 System.out.println("The state is "+mediaPlayer.isPlayerReady());
		 while(mediaPlayer.isPlayerReady()){
				 mediaPlayer.start();
				 reloadFrame();
				 Thread.sleep(5000); 
			
		 }
	 }
	 
	 
	 
	 
	 private void playerOn()throws Exception{
		 while (true) {
			 createAndRun();
			 //mediaPlayer.setPlayerReady(Boolean.TRUE);
			 
			 System.out.println(">>>>>>>> Restart >>>>>>>>>>");
			 Thread.sleep(5000);
		}
		 
	 }
	
	 

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			MediaPlayerUtil mediaPlayerUtil = new MediaPlayerUtil();
			mediaPlayerUtil.createAndRun();
			//mediaPlayerUtil.playerOn();
			
			System.out.println("The process is end");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
